<template>
    <div id="OrdersTitle">
        <v-details :inputConfig="inputConfig" :detailData="inputData" :rules="rules" @pop="setdata"
                   ref="input"></v-details>
    </div>
</template>

<script>
import details from 'commons/Detail'
import baseMixin from "@/mixins/BaseMixin";

export default {
    props: {
        inputData: {
            type: Object,
        }
    },
    mixins: [baseMixin],
    name: "OrdersTitle",
    data() {
        return {
            newFlag: false,
            addflag: false
        }
    },
    computed: {
        inputConfig() {
            return [
                [
                    {
                        type: "table",
                        label: this.$t('outbound.user'),
                        props: "userkey",
                        tableConfig: [
                            {
                                label: this.$t('security.userName'),
                                prop: "username",
                                input: "input",
                            },
                            {
                                label: this.$t('outbound.userFullName'),
                                prop: "fullname",
                                input: "input",
                            },
                            {
                                label: this.$t('app.email'),
                                prop: "email",
                                input: "input",
                            },
                            {
                                label: this.$t('common.status'),
                                prop: "status",
                                input: "select",
                                options: [
                                    {code: "1", description: "可用"},
                                    {code: "-1", description: "不可用"},
                                ]
                            }
                        ],
                        url: `/user1/`,
                        disabled: this.addflag
                    },
                    {
                        type: "input",
                        label: this.$t('outbound.usergroup'),
                        props: "usergroup",
                        disabled: true,
                    }
                ],
            ]
        },
        rules() {
            return {
                userkey: [
                    {required: true, validator: this.userkey, trigger: 'blur'},
                ]
            }
        }
    },
    methods: {
        userkey(rule, value, callback) {
            if (value === '') {
                callback(new Error());
            } else {
                this.$api.get(`/isExist/${value}`).then(res => {
                    
                        if (res.bean) {
                            this.inputData.usr_lname = res.bean.fullname;
                            callback();
                        } else {
                            this.$message.error(this.$t('msg.msg49') + `:${value}`);
                            this.inputData[rule.field] = "";
                            callback(new Error());
                        }
                }).catch(e => {
                    this.$message.error(e);
                    callback(new Error())
                })
            }
        },
        setdata(data, id, close, listId) {
            this.inputData.userkey = data.username;
            this.inputData.usr_lname = data.fullname;
            close(false);
        },
    },
    mounted() {
        this.newFlag = this.$store.state.newFlag;
        if (Object.keys(this.$store.state.skip).length !== 0) {
            let data = this.$store.state.skip;
            if (date.newFlag) {
                this.addflag = false;
            } else {
                this.addflag = true;
            }
        } else if (this.newFlag) {
            this.addflag = false;
        } else {
            this.addflag = true;
        }
    },
    watch: {},
    components: {
        'v-details': details
    }
}
</script>

<style scoped>

</style>
